<?php
declare (strict_types=1);

namespace app\command;

use app\server\CallServer;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use \think\facade\Db;
use think\facade\Log;

class Call extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('LogHap')
            ->setDescription('the check command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            $this->call();
        } catch (\Exception $e) {
            addDebugLog('CallLog', ['msg' => $e->getMessage()]);
            var_dump($e->getMessage());
        }
        $output->writeln('call');
    }


    public function call()
    {
        $table = 'app_ad_event';
        $list = Db::table($table)->field('id,hap')->where(['call_status'=>0])->order("id asc")->limit(2000)->select()->toArray();
        $isCallId = $failCallId = [];
        foreach($list as $value){
            $hapArr = json_decode($value['hap'],true);
            if(!empty($hapArr['campaign_id'])){
                $isCall = app(CallServer::class)->adsCall($hapArr);
                if($isCall){
                    $isCallId[] = $value['id'];
                }else{
                    $failCallId[] = $value['id'];
                }
            }else{
                $failCallId[] = $value['id'];
            }
        }
        if(!empty($isCallId)){
            edit($table,[['id','in',$isCallId]],['call_status'=>1]);
        }
        if(!empty($failCallId)){
            edit($table,[['id','in',$failCallId]],['call_status'=>2]);
        }
    }



}
